// 添加 或者 修改 url中参数的值
function clearURIExceptPageNow(){
    return document.location.href.split('&')[0]
}

function getUrlParam(thisURL, name) {
    let array = new RegExp(name + '=([^&]*)').exec(thisURL);
    return (array == null || array.length < 1)? '' : decodeURI(array[1]);
}

function updateUrlParam(thisURL, name, val) {
    thisURL = decodeURI(thisURL)
    val = decodeURI(val)
    // 如果 url中包含这个参数 则修改
    if (thisURL.indexOf(name+'=') > 0) {
        var v = getUrlParam(thisURL, name);
        if (v != null) {
            // 是否包含参数
            thisURL = thisURL.replace(name + '=' + v, name + '=' + val);
        }
        else {
            thisURL = thisURL.replace(name + '=', name + '=' + val);
        }

    } // 不包含这个参数 则添加
    else {
        if (thisURL.indexOf("?") > 0) {
            thisURL = thisURL + "&" + name + "=" + val;
        }
        else {
            thisURL = thisURL + "?" + name + "=" + val;
        }
    }
    return thisURL;
}
function updateCurrentUrlParam(name, val) {
    return updateUrlParam(document.location.href, name, val)
}
function getUrlParamToMap() {
    const params = window.location.toString().split('\?')[1]
    let map = new Map();
    if(params == null || params === '') return ''
    params.split('&').forEach(param =>{
        let kv = param.split('=');
        let k = kv[0]
        let v = kv[1]
        map.set(k,v)
    })
    return map
}
function getUrlParamToJSON() {
    let obj= Object.create(null)
    let map = getUrlParamToMap();
    if(map == null || map === '') return;
    for (let[k,v] of map) {
        k = decodeURI(k)
        v = decodeURI(v)
        obj[k] = v;
    }
    return JSON.stringify(obj)
}
